package com.qpx.jxcbiz.web;


import com.alipay.api.domain.MutipleCurrencyDetail;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.qpx.jxc.common.dto.GoodDto;
import com.qpx.jxcbiz.config.Config1;
import com.qpx.jxcbiz.config.Config2;
import com.qpx.jxcbiz.entity.TGoods;
import com.qpx.jxcbiz.service.ITGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.beans.IntrospectionException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-05-22
 */
@RestController
@RequestMapping("/tGoods")
@Slf4j
public class TGoodsController {


    @Value("${spring.cloud.nacos.discovery.server-addr}")
    private String address;

    @Autowired
    private Config1 config1;

    @Autowired
    private Config2 config2;


    @Autowired
    ITGoodsService itGoodsService;

    @RequestMapping("/test")
    public Object test() {
        Wrapper wrapper = new EntityWrapper();
        List list = itGoodsService.selectList(wrapper);
        log.info("address:{}", address);
        log.info("config1.id:{}", config1.getId());
        log.info("config2.address:{}", config2.getAddress());
        return list;
    }


    @RequestMapping("/downLoadGoodsExcel")
    public ResponseEntity<byte[]> downLoadGoodsExcel(GoodDto goodDto) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, ParseException, InvocationTargetException, IOException, IOException {
        log.info("开始打印导出excel 的 日志....");
        XSSFWorkbook xssfSheets = itGoodsService.downLoadGoodsExcel(goodDto);
        log.info("--------------------------------结果是{}:{}",xssfSheets,1);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        xssfSheets.write(byteArrayOutputStream);

        HttpHeaders httpHeaders =  new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment", new String("商品表.xlsx".getBytes("UTF-8"), "ISO-8859-1"));
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity(byteArrayOutputStream.toByteArray(),httpHeaders, HttpStatus.CREATED);

}

   @RequestMapping("/updateLoad")
   public void uploadExcel(MultipartFile file) throws Exception {
     itGoodsService.uploadExcel(file.getInputStream(),file.getOriginalFilename());
   }


}
